package com.peakmain.wanandroid.loginmodule.presenter;

import android.annotation.SuppressLint;

import com.peakmain.wanandroid.loginmodule.contract.RegisterContract;
import com.peakmain.wanandroid.netmodule.base.BasePresenter;
import com.peakmain.wanandroid.netmodule.net.ApiService;
import com.peakmain.wanandroid.netmodule.net.RetrofitManager;
import com.peakmain.wanandroid.netmodule.net.RxSchedulers;
import com.peakmain.wanandroid.netmodule.bean.DataResponse;

import javax.inject.Inject;

import io.reactivex.functions.Consumer;

/**
 * author: peakmain
 * createdata：2019/7/10
 * mail: 2726449200@qq.com
 * desiption:
 */
public class RegisterPresenter extends BasePresenter<RegisterContract.View> implements RegisterContract.Presenter {
    @Inject
    public RegisterPresenter() {

    }

    @SuppressLint("CheckResult")
    @Override
    public void register(String account, String password, String rePassword) {
        mView.showLoading();
        RetrofitManager.create(ApiService.class).register(account, password, rePassword)
                .compose(mView.<DataResponse>bindToLife())
                .compose(RxSchedulers.<DataResponse>applySchedulers())
                .subscribe(new Consumer<DataResponse>() {
                    @Override
                    public void accept(DataResponse dataResponse) throws Exception {

                        if (dataResponse.getErrorCode() != 0) {
                            mView.showFaild(dataResponse.getErrorMessage());
                        } else {
                            mView.showRegisterSuccess();
                        }
                        mView.hideLoading();
                    }
                }, new Consumer<Throwable>() {
                    @Override
                    public void accept(Throwable throwable) throws Exception {
                        mView.showFaild("网络错误，请稍后重试");
                        mView.hideLoading();
                    }
                });
    }
}

